package com.xxl.job.admin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xxl.job.admin.core.model.JobInfo;
import com.xxl.job.admin.core.model.JobLogGlue;
import com.xxl.job.admin.dao.IJobInfoDao;
import com.xxl.job.admin.dao.IJobLogGlueDao;
import com.xxl.job.core.glue.GlueTypeEnum;

/**
 * job code controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
@RequestMapping("/jobcode")
public class JobCodeController {
	
	@Resource
	private IJobInfoDao xxlJobInfoDao;
	@Resource
	private IJobLogGlueDao xxlJobLogGlueDao;

	@RequestMapping
	public String index(Model model, Long jobId) {
		JobInfo jobInfo = xxlJobInfoDao.loadById(jobId);
		List<JobLogGlue> jobLogGlues = xxlJobLogGlueDao.findByJobId(jobId);

		if (jobInfo == null) {
			throw new RuntimeException("抱歉，任务不存在.");
		}
		if (GlueTypeEnum.BEAN == GlueTypeEnum.match(jobInfo.getGlueType())) {
			throw new RuntimeException("该任务非GLUE模式.");
		}

		// Glue类型-字典
		model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());

		model.addAttribute("jobInfo", jobInfo);
		model.addAttribute("jobLogGlues", jobLogGlues);
		return "jobcode/jobcode.index.ftl";
	}
	
}
